package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

	@Reference
	private CheckItemService checkItemService;

	/**
	 * 新增检查项
	 * @param checkItem
	 */
	@RequestMapping("/add")
	public Result add(@RequestBody CheckItem checkItem){

		try {
			checkItemService.add(checkItem);
		} catch (Exception e) {
			//调用新增方法失败
			e.printStackTrace();
			Result result = new Result(false,MessageConstant.ADD_CHECKITEM_FAIL);
			return result;
		};
		Result result = new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
		return result;
	}

	/**
	 * 分页查询所有检查项
	 * @return
	 */
	@RequestMapping("/pageQuery")
	public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean){

		PageResult pageResult = checkItemService.pageQuery(queryPageBean);

		return pageResult;
	}

	/**
	 * 删除检查项
	 * @param id
	 */
	@RequestMapping("/delete")
	@PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
	public Result delete(Integer id){
		System.out.println("id---"+id);

		try {
			checkItemService.delete(id);
			Result result = new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			Result result = new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
			return result;
		}

	}

	/**
	 * 根据id查询检查项目
	 * @param id
	 * @return
	 */
	@RequestMapping("/queryItemById")
	public Result queryItemById(Integer id){
		try {
			CheckItem item = checkItemService.queryItemById(id);
			Result result = new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,item);
			return result;

		} catch (Exception e) {
			e.printStackTrace();
			Result result = new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
			return result;
		}

	}
	/**
	 * 编辑检查项
	 * @param checkItem
	 * @return
	 */
	@RequestMapping("/edit")
	public Result editCheckItem(@RequestBody CheckItem checkItem){

		try {
			checkItemService.edit(checkItem);
		} catch (Exception e) {
			e.printStackTrace();
			Result result = new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
			return result;
		}

		Result result = new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
		return result;
	}

	/**
	 * 查询所有检查项
	 * @return
	 */
	@RequestMapping("/findAll")
	public Result findAll(){
		try {
			List<CheckItem> itemList = checkItemService.findAll();
			Result result = new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,itemList);
			return result;

		} catch (Exception e) {
			e.printStackTrace();
			Result result = new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
			return result;
		}

	}

	/**
	 * 查询中间表
	 * @param
	 * @return
	 */
	@RequestMapping("/findCheckitemsIdsByCheckgroupId")
	public Result findCheckitemsIdsByCheckgroupId(Integer checkgroupId){
		try {
			List<Integer> ids = checkItemService.findCheckitemsIdsByCheckgroupId(checkgroupId);
			return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,ids);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
		}
	}





}
